# -*- coding: utf-8 -*-
# @Time     :2021/7/23 12:06
# @Author   : master
# @Software : PyCharm


import time
import self as self
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.common.exceptions import *  # 导入所有的异常类
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from urllib3.util import timeout


def browser():

    driver = webdriver.Chrome()
    return driver


url1 = 'http://gis.luhongkj.com/#/login'


class luhong(object):

    def __init__(self):

        self.driver = browser()

    def open(self, url, t='', timeout=10):
        '''
        使用 get 打开 url 后，最大化窗口，刞断 title 符合预期
        Usage:
        driver = Yoyo()
        driver.open(url,t='')
        '''
        self.driver.get(url)
        self.driver.maximize_window()

        try:
            WebDriverWait(self.driver, timeout,
                          1).until(EC.title_contains(t))

        except TimeoutException:
            print("open %s title error" % url)

        except Exception as msg:
            print("Error:%s" % msg)

    def find_element(self, locator, timeout=10):
        '''
        定位元素，参数 locator 是元祖类型
        Usage:
        locator = ("id","xxx")
        driver.find_element(locator)
        '''
        element = WebDriverWait(
            self.driver, timeout, 1).until(
            EC.presence_of_element_located(locator))
        return element

    def find_elements(self, locator, timeout=10):
        '''定位一组元素'''
        elements = WebDriverWait(
            self.driver, timeout, 1).until(
            EC.presence_of_all_elements_located(locator))
        return elements

    def click(self, locator):
        '''
        点击操作
        Selenium 高级自动化（上海-悠悠、葵花）
        Usage:
        locator = ("id","xxx")
        driver.click(locator)
        '''
        element = self.find_element(locator)
        element.click()

    def send_keys(self, locator, text):
        '''
        収送文本，清空后输入
        Usage:
        locator = ("id","xxx")
        driver.send_keys(locator, text)
        '''
        element = self.find_element(locator)
        element.clear()
        element.send_keys(text)

    def is_text_in_element(self, locator, text, timeout=10):
        '''
        刞断文本在元素里,没定位刡元素迒回 False，定位刡迒回刞
        断结果布尔值
        Selenium 高级自动化（上海-悠悠、葵花）
        result = driver.text_in_element(locator, text)
        '''
        try:
            result = WebDriverWait(
                self.driver, timeout, 1).until(
                EC.text_to_be_present_in_element(
                    locator, text))
        except TimeoutException:
            print("元素没定位刡：" + str(locator))
            return False
        else:
            return result

    def is_text_in_value(self, locator, value, timeout=10):
        '''
        刞断元素的 value 值，没定位刡元素迒回 false,定位刡迒回刞
        断结果布尔值
        result = driver.text_in_element(locator, text)
        '''
        try:
            result = WebDriverWait(
                self.driver, timeout, 1).until(
                    EC.text_to_be_present_in_element_value(
                        locator, value))
        except TimeoutException:
            print("元素没定位刡：" + str(locator))
            return False
        else:
            return result

    def is_title(self, title, timeout=10):
        '''判断 title 完全等于'''
        result = WebDriverWait(self.driver, timeout,
                               1).until(EC.title_is(title))
        return result

    def is_title_contains(self, title, timeout=10):
        '''判断 title 包含'''
        result = WebDriverWait(self.driver, timeout,
                               1).until(EC.title_contains(title))
        return result

    def is_selected(self, locator, timeout=10):
        '''判断元素被选中，返回布尔值,'''
        result = WebDriverWait(
            self.driver, timeout, 1).until(
            EC.element_located_to_be_selected(locator))
        return result

    def is_selected_be(self, locator, selected=True,
                       timeout=10):
        '''判断元素的状态，selected 是期望的参数 true/False
        返回布尔值'''
        result = WebDriverWait(
            self.driver, timeout, 1).until(
                EC.element_located_selection_state_to_be(
                    locator, selected))
        return result

    def is_alert_present(self, timeout=10):
        '''判断页面是否有 alert，
        有返回alert(注意返里是返回 alert,不是 True)
        没有返回 False'''
        result = WebDriverWait(self.driver, timeout,
                               1).until(EC.alert_is_present())
        return result

    def is_visibility(self, locator, timeout=10):
        '''元素可见返回本身，不可见返回 Fasle'''

        result = WebDriverWait(
            self.driver, timeout, 1).until(
            EC.visibility_of_element_located(locator))
        return result

    def is_invisibility(self, locator, timeout=10):
        '''元素可见返回本身，不可见返回 True，没找到元素也返回
        True'''
        result = WebDriverWait(
            self.driver, timeout, 1).until(
            EC.invisibility_of_element_located(locator))
        return result

    def is_clickable(self, locator, timeout=10):
        '''元素可以点击 is_enabled 返回本身，不可点击迒回 Fasle'''
        result = WebDriverWait(self.driver, timeout,
                               1).until(EC.element_to_be_clickable(locator))
        return result

    def is_located(self, locator, timeout=10):
        '''判断元素有没被定位到（并不意味着可见），定位到返回
        element,没定位到返回 False'''
        result = WebDriverWait(
            self.driver, timeout, 1).until(
            EC.presence_of_element_located(locator))
        return result

    def move_to_element(self, locator):
        '''
        鼠标悬停操作
        Usage:
        locator = ("id","xxx")
        driver.move_to_element(locator)
        '''
        element = self.find_element(locator)
        ActionChains(self.driver).move_to_element(element).perform()

    def back(self):
        """
        Back to old window.
        Usage:
        driver.back()
        """
        self.driver.back()

    def forward(self):
        """
        Forward to old window.
        Usage:
        driver.forward()
        """
        self.driver.forward()

    def close(self):
        """
        Close the windows.
        Usage:
        driver.close()
        """
        self.driver.close()

    def quit(self):
        """
        Quit the driver and close all the windows.
        Usage:
        driver.quit()
        """
        self.driver.quit()

    def get_title(self):
        '''获取 title'''
        return self.driver.title

    def get_text(self, locator):
        '''获取文本'''
        element = self.find_element(locator)
        return element.text

    def get_attribute(self, locator, name):
        '''获叏属性'''
        element = self.find_element(locator)
        return element.get_attribute(name)

    def js_execute(self, js):
        '''执行 js'''
        return self.driver.execute_script(js)

    def js_focus_element(self, locator):
        '''聚焦元素'''
        target = self.find_element(locator)
        self.driver.execute_script("arguments[0].scrollIntoView();", target)

    def js_scroll_top(self):
        # '''滚动到顶部'''

        js = "window.scrollTo(0,0)"
        self.driver.execute_script(js)

    def js_scroll_end(self):
        '''滚动到底部'''
        js = "window.scrollTo(0,document.body.scrollHeight)"
        self.driver.execute_script(js)

    def select_by_index(self, locator, index):
        '''通过索引,index 是索引第几个，从 0 开始'''
        element = self.find_element(locator)
        Select(element).select_by_index(index)

    def select_by_value(self, locator, value):
        '''通过 value 属性'''
        element = self.find_element(locator)
        Select(element).select_by_value(value)

    def select_by_text(self, locator, text):
        '''通过文本值定位'''
        element = self.find_element(locator)
        Select(element).select_by_value(text)

    def lougin(self):
        self.send_keys(
            ('xpath',
             "//input[@placeholder='请输入帐号']"),
            13012345673)
        self.send_keys(
            ('xpath',
             "//input[@placeholder='请输入密码']"),
            13012345673)
        self.click(
            ('xpath', '//*[@id="app"]/div/div[2]/div[2]/form/button/span'))


if __name__ == '__main__':
    # if 下面的代码都是测试调试的代码，自测内容
    driver = browser()
    driver_n = luhong()  # 迒回类的实例：打开浏览器
    driver_n.open("http://gis.luhongkj.com/#/login")
